package com.wtf.dsp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wtf.dsp.admin.entity.WtfDspCodeTemplate;
import com.wtf.dsp.admin.mapper.WtfDspCodeTemplateMapper;
import com.wtf.dsp.admin.model.dto.*;
import com.wtf.dsp.admin.model.vo.TemplateConfigVo;
import com.wtf.dsp.admin.service.CodeTemplateService;
import com.wtf.dsp.core.constant.NsConstant;
import com.wtf.dsp.core.util.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: CodeTemplateServiceImpl
 * Description:
 * date: 2024/3/6 17:34
 *
 * @author linhuanyou
 * @version 1.0
 */
@Slf4j
@Service
public class CodeTemplateServiceImpl implements CodeTemplateService {

    @Resource
    private WtfDspCodeTemplateMapper wtfDspCodeTemplateMapper;

    @Override
    public int add(TemplateAddDTO templateAddDTO) {
        WtfDspCodeTemplate wtfDspCodeTemplate = new WtfDspCodeTemplate();
        BeanUtils.copyProperties(templateAddDTO,wtfDspCodeTemplate);
        int insert = wtfDspCodeTemplateMapper.insert(wtfDspCodeTemplate);
        return insert;
    }

    @Override
    public PageUtils page(TemplatePageDTO templateListDTO) {
        if(StringUtils.isBlank(templateListDTO.getNs())){
            templateListDTO.setNs(NsConstant.DEFAULT_NS);
        }
        Page<?> page = PageHelper.startPage(templateListDTO.getPage(), templateListDTO.getLimit());
        List<TemplateConfigVo> list = wtfDspCodeTemplateMapper.page(templateListDTO);
        int total = (int) page.getTotal();
        return new PageUtils(list, total, templateListDTO.getLimit(),templateListDTO.getPage());
    }

    @Override
    public int update(TemplateUpdateDTO templateUpdDTO) {
        WtfDspCodeTemplate wtfTemplateConfig = new WtfDspCodeTemplate();
        BeanUtils.copyProperties(templateUpdDTO,wtfTemplateConfig);
        int update = wtfDspCodeTemplateMapper.updateById(wtfTemplateConfig);
        return update;
    }

    @Override
    public int remove(TemplateDelDTO templateDelDTO) {
        LambdaQueryWrapper<WtfDspCodeTemplate> wtfTemplateConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wtfTemplateConfigLambdaQueryWrapper.eq(WtfDspCodeTemplate::getNs,templateDelDTO.getNs());
        wtfTemplateConfigLambdaQueryWrapper.eq(WtfDspCodeTemplate::getDs,templateDelDTO.getDs());
        wtfTemplateConfigLambdaQueryWrapper.in(WtfDspCodeTemplate::getId,templateDelDTO.getIds());
        return wtfDspCodeTemplateMapper.delete(wtfTemplateConfigLambdaQueryWrapper);
    }

    @Override
    public List<TemplateConfigVo> list(TemplateListDTO templateListDTO) {
        LambdaQueryWrapper<WtfDspCodeTemplate> wtfTemplateConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wtfTemplateConfigLambdaQueryWrapper.eq(WtfDspCodeTemplate::getNs,templateListDTO.getNs());
        wtfTemplateConfigLambdaQueryWrapper.eq(WtfDspCodeTemplate::getDs,templateListDTO.getDs());
        List<TemplateConfigVo> list = wtfDspCodeTemplateMapper.selectList(wtfTemplateConfigLambdaQueryWrapper).stream().map(e->{
            TemplateConfigVo templateConfigVo = new TemplateConfigVo();
            BeanUtils.copyProperties(e,templateConfigVo);
            return templateConfigVo;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public TemplateConfigVo info(Integer id) {
        WtfDspCodeTemplate wtfDspCodeTemplate = wtfDspCodeTemplateMapper.selectById(id);
        TemplateConfigVo templateConfigVo = new TemplateConfigVo();
        BeanUtils.copyProperties(wtfDspCodeTemplate,templateConfigVo);
        return templateConfigVo;
    }

}
